/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.unix;

import java.io.File;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.util.Misc;

/**
 * This class provides implementation for file statistics-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Stat {

    /**
     * No instance of this class.
     */
    private Stat() {
    } // end empty constructor

    /**
     * Returns file statistics.
     * @param ctxt context
     * @param path file path
     * @return file statistics
     * @throws Fail.Exception if file does not exist
     */
    @Primitive
    public static Value unix_stat(final CodeRunner ctxt,
                                  final Value path)
        throws Fail.Exception {
        return createStats(ctxt, false, ctxt.getContext().getRealFile(path));
    } // end method 'unix_stat(CodeRunner, Value)'

    /**
     * Exact synonym of {@link fr.x9c.cadmium.primitives.unix.Stat#unix_stat(fr.x9c.cadmium.kernel.CodeRunner, fr.x9c.cadmium.kernel.Value)}.
     * @param ctxt context
     * @param path file path
     * @return file statistics
     * @throws Fail.Exception if file does not exist
     */
    @Primitive
    public static Value unix_lstat(final CodeRunner ctxt,
                                   final Value path)
        throws Fail.Exception {
        return unix_stat(ctxt, path);
    } // end method 'unix_lstat(CodeRunner, Value)'

    /**
     * No implementation of this primitive.
     * @param ctxt context
     * @param fd ignored
     * @return <i>unit</i>
     * @throws Fail.Exception always
     */
    @Primitive
    public static Value unix_fstat(final CodeRunner ctxt,
                                   final Value fd)
        throws Fail.Exception {
        Fail.invalidArgument("Unix.fstat not implemented");
        return Value.UNIT;
    } // end method 'unix_fstat(CodeRunner, Value)'

    /**
     * Returns file statistics.
     * @param ctxt context
     * @param path file path
     * @return file statistics
     * @throws Fail.Exception if file does not exist
     */
    @Primitive
    public static Value unix_stat_64(final CodeRunner ctxt,
                                     final Value path)
        throws Fail.Exception {
        return createStats(ctxt, true, ctxt.getContext().getRealFile(path));
    } // end method 'unix_stat_64(CodeRunner, Value)'

    /**
     * Exact synonym of {@link fr.x9c.cadmium.primitives.unix.Stat#unix_stat_64(fr.x9c.cadmium.kernel.CodeRunner, fr.x9c.cadmium.kernel.Value)}.
     * @param ctxt context
     * @param path file path
     * @return file statistics
     * @throws Fail.Exception if file does not exist
     */
    @Primitive
    public static Value unix_lstat_64(final CodeRunner ctxt,
                                      final Value path)
        throws Fail.Exception {
        return unix_stat_64(ctxt, path);
    } // end method 'unix_lstat_64(CodeRunner, Value)'

    /**
     * No implementation of this primitive.
     * @param ctxt context
     * @param fd ignored
     * @return <i>unit</i>
     * @throws Fail.Exception always
     */
    @Primitive
    public static Value unix_fstat_64(final CodeRunner ctxt,
                                      final Value fd)
        throws Fail.Exception {
        Fail.invalidArgument("Unix.fstat_64 not implemented");
        return Value.UNIT;
    } // end method 'unix_fstat_64(CodeRunner, Value)'

    /**
     * Creates a statistics record.
     * @param ctxt context
     * @param use64 whether 64-bit integer should be used
     * @param f file to get statistics from
     * @return statistics record
     * @throws Fail.Exception if file does not exist
     */
    private static Value createStats(final CodeRunner ctxt,
                                     final boolean use64,
                                     final File f)
        throws Fail.Exception {
        if (!f.exists()) {
            Unix.fail(ctxt, "utimes", "file does not exist");
            return Value.UNIT;
        } // end if
        final double time =
            ((double) f.lastModified()) / Unix.MILLISECS_PER_SEC;
        final Value offset;
        if (use64) {
            final Block ofs = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            ofs.setInt64(f.length());
            offset = Value.createFromBlock(ofs);
        } else {
            offset = Value.createFromLong((int) f.length());
        } // end if/else

        final Block res = Block.createBlock(12, 0);
        res.set(0, Value.ZERO);
        res.set(1, Value.ZERO);
        res.set(2, f.isDirectory() ? Value.ONE : Value.ZERO);
        res.set(3, Value.createFromLong(Misc.getPermissions(f)));
        res.set(4, Value.ZERO);
        res.set(5, Value.ONE);
        res.set(6, Value.ONE);
        res.set(7, Value.ZERO);
        res.set(8, offset);
        res.set(9, Value.createFromBlock(Block.createDouble(time)));
        res.set(10, Value.createFromBlock(Block.createDouble(time)));
        res.set(11, Value.createFromBlock(Block.createDouble(time)));
        return Value.createFromBlock(res);
    } // end method 'createStats(CodeRunner, boolean, File)'

} // end class 'Stat'
